﻿Public Class frmOfertySzukaj

    Private mvarOferta As clsSzukanieOferty
    Private mvarDbHolder As clsDataBase
    Private mvarSzukanieOferty As clsSzukanieOferty
    Private mvarUzytkownik As clsUzytkownik
    Private mvarIdKlienta As Integer = -1
    Private mvarCommand As String

    Public Sub New()

        ' To wywołanie jest wymagane przez projektanta.
        InitializeComponent()

        ' Dodaj inicjację po wywołaniu funkcji InitializeComponent().
        mvarDbHolder = New clsDataBase(VAR_CONNECTION_PARAMETERS, VAR_ERROR_HANDLER)

        mvarUzytkownik = clsUzytkownik.Instance()

        PobierzWszystkieGatunkiMaterialu()
        PobierzStatusOferty()
        Uprawnienia()

    End Sub

    Private Sub Uprawnienia()

        If mvarUzytkownik.Uprawnienia("biuro") = 0 Then
            cmdNowaOferta.Enabled = False
            cmdEdycjaOferty.Enabled = False
            cmdKopiuj.Enabled = False
        End If

        If mvarUzytkownik.Uprawnienia("biuro") = 1 Then
            cmdKopiuj.Enabled = False
        End If

    End Sub

  

  
    Private Sub PobierzWszystkieGatunkiMaterialu()

        Dim lvarDataTable As New DataTable

        lvarDataTable = clsGatunek.PobierzWszystkieGatunki(mvarDbHolder, VAR_ERROR_HANDLER)

        If Not lvarDataTable Is Nothing Then
            For Each lvarRow As DataRow In lvarDataTable.Rows
                cmbGatunek.Items.Add(New clsGatunek(CInt(lvarRow("id")), IIf(IsDBNull(lvarRow("nazwa_ang")), "", lvarRow("nazwa_ang")).ToString(), IIf(IsDBNull(lvarRow("nazwa")), "", lvarRow("nazwa")).ToString(), IIf(IsDBNull(lvarRow("nazwa_niem")), "", lvarRow("nazwa_niem")).ToString(), IIf(IsDBNull(lvarRow("norma_wykonania")), "", lvarRow("norma_wykonania")).ToString(), IIf(IsDBNull(lvarRow("stop_id")), -1, lvarRow("stop_id")).ToString()))
            Next
        End If

    End Sub

    Private Sub PobierzStatusOferty()

        Dim lvarDataTable As New DataTable

        lvarDataTable = clsStatusOferty.PobierzWszystkieOpcje(mvarDbHolder, VAR_ERROR_HANDLER)

        If Not lvarDataTable Is Nothing Then
            For Each lvarRow As DataRow In lvarDataTable.Rows
                cmbStatusOferty.Items.Add(New clsStatusOferty(CInt(lvarRow("id")), CType(IIf(IsDBNull(lvarRow("nazwa")), "", lvarRow("nazwa")), String)))
            Next
        End If

    End Sub

    Private Sub txtNazwaKlienta_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtNazwaKlienta.DoubleClick
        Dim mvarFrmSzukaniaKlienta As frmKlientSzukanie
        mvarFrmSzukaniaKlienta = New frmKlientSzukanie(True, mvarIdKlienta)
        mvarFrmSzukaniaKlienta.ShowDialog()
        If mvarFrmSzukaniaKlienta.dgvInformacje.SelectedRows.Count > 0 Then
            txtNazwaKlienta.Text = mvarFrmSzukaniaKlienta.dgvInformacje.SelectedRows.Item(0).Cells("colNazwaSkrocona").Value.ToString()
            mvarIdKlienta = CInt(mvarFrmSzukaniaKlienta.dgvInformacje.SelectedRows.Item(0).Cells("colIdKlienta").Value)
        Else
            txtNazwaKlienta.Text = String.Empty
            mvarIdKlienta = -1
        End If
    End Sub


    Private Sub cmdWyczyscSzczegoly_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdWyczyscSzczegoly.Click

        txtNumerOferty.Text = String.Empty
        txtNazwaKlienta.Text = String.Empty
        dtpDataOfertyOd.Text = Now.ToShortDateString()
        dtpDataOfertyDo.Text = Now.ToShortDateString()
        chbDataOferty.Checked = False
        cmbGatunek.SelectedIndex = -1
        txtWymiar.Text = String.Empty
        cmbStatusOferty.SelectedIndex = -1

    End Sub

    Private Sub cmdSzukaj_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSzukaj.Click

        Dim lvarCommand As String
        Dim lvarCommand1 As String
        Dim lvarI As Integer
        Dim lvarWyzn As Boolean = False
        Dim lvarWyzn1 As Boolean = False

        If mvarCommand <> String.Empty Then
            lvarCommand = mvarCommand
            lvarWyzn = True
            mvarCommand = String.Empty
        End If
        If txtNumerOferty.Text = String.Empty And txtNazwaKlienta.Text = String.Empty And dtpDataOfertyOd.Checked = False And dtpDataOfertyDo.Checked = False And chbDataOferty.Checked = False And cmbGatunek.SelectedIndex = -1 And txtWymiar.Text = String.Empty And cmbStatusOferty.SelectedIndex = -1 Then
            MessageBox.Show("Wybierz kryterium wyszukiwania", "WIREMET")
        Else
            If dtpDataOfertyOd.Value.Date > dtpDataOfertyDo.Value.Date Then
                MessageBox.Show("Wybierz poprawnie kryterium wyszukiwania", "WIREMET")
            Else
                If txtNumerOferty.TextLength > 0 Then
                    If lvarWyzn = True Then
                        lvarCommand = lvarCommand + " AND"
                    End If
                    lvarCommand = lvarCommand + " oferta.numer='" & txtNumerOferty.Text & "'"
                    lvarWyzn = True
                End If
                If txtNazwaKlienta.Text <> String.Empty Then
                    If lvarWyzn = True Then
                        lvarCommand = lvarCommand + " AND"
                    End If
                    lvarCommand = lvarCommand + " UPPER (klient.nazwa_skrocona) LIKE UPPER ('" & txtNazwaKlienta.Text & "%')"
                    lvarWyzn = True
                End If
                If cmbStatusOferty.SelectedIndex <> -1 Then
                    If lvarWyzn = True Then
                        lvarCommand = lvarCommand + " AND"
                    End If
                    lvarCommand = lvarCommand + " status_oferty.nazwa='" & cmbStatusOferty.SelectedItem.ToString & "'"
                    lvarWyzn = True
                End If
                If dtpDataOfertyOd.Checked Or dtpDataOfertyDo.Checked Or chbDataOferty.Checked Then
                    If lvarWyzn = True Then
                        lvarCommand = lvarCommand + " AND"
                    End If
                    lvarCommand = lvarCommand + " oferta.data_oferty::date between '" & dtpDataOfertyOd.Value.Date & "' AND '" & dtpDataOfertyDo.Value.Date & "'"
                    lvarWyzn = True
                End If
                If cmbGatunek.SelectedIndex <> -1 Then
                    If lvarWyzn = True Then
                        lvarCommand1 = lvarCommand1 + " AND"
                    End If
                    lvarCommand1 = lvarCommand1 + " oferta.id IN (SELECT oferta_id FROM oem.pozycje_oferty WHERE "
                    lvarCommand1 = lvarCommand1 + " gatunek_id=" & CType(cmbGatunek.SelectedItem, clsGatunek).Id
                    lvarWyzn1 = True
                End If
                If txtWymiar.Text <> String.Empty Then
                    If lvarWyzn1 = True Then
                        lvarCommand1 = lvarCommand1 + " AND"
                    Else
                        If lvarWyzn = True Then
                            lvarCommand1 = lvarCommand1 + " AND"
                        End If
                        lvarCommand1 = lvarCommand1 + " oferta.id IN (SELECT oferta_id FROM oem.pozycje_oferty WHERE "
                    End If

                    lvarCommand1 = lvarCommand1 + " wymiar='" & txtWymiar.Text & "'"
                    lvarWyzn1 = True
                End If
                If cmbGatunek.SelectedIndex <> -1 Or txtWymiar.Text <> String.Empty Then
                    lvarCommand1 = lvarCommand1 + ")"
                End If

                mvarSzukanieOferty = New clsSzukanieOferty
                mvarSzukanieOferty.Search(mvarDbHolder, VAR_ERROR_HANDLER, lvarCommand, lvarCommand1)

                dgvOferty.Rows.Clear()
                If mvarSzukanieOferty.Oferty.Rows.Count = 0 Then
                    MessageBox.Show("Nie ma takich ofert.", "WIREMET")
                Else
                    For Each lvarRow As DataRow In mvarSzukanieOferty.Oferty.Rows
                            lvarI += 1
                            dgvOferty.Rows.Add(lvarI, lvarRow.Item("id_oferty"), lvarRow.Item("numer_oferty"), lvarRow.Item("klient_nazwa"), CType(lvarRow.Item("data_oferty"), Date).ToShortDateString, lvarRow.Item("status_nazwa"))
                            Select Case CType(lvarRow.Item("data_waznosci"), Date).ToShortDateString
                            Case Date.Today.ToShortDateString()
                                dgvOferty.Rows.Item(lvarI - 1).DefaultCellStyle.BackColor = Color.Yellow
                            Case Is < DateSerial(Today.Year, Today.Month, Today.Day).ToShortDateString()
                                dgvOferty.Rows.Item(lvarI - 1).DefaultCellStyle.BackColor = Color.Red
                        End Select
                    Next
                End If
            End If
        End If

    End Sub

    Private Sub dgvOferty_CellMouseDoubleClick(ByVal sender As Object, ByVal e As DataGridViewCellMouseEventArgs) Handles dgvOferty.CellMouseDoubleClick

        If dgvOferty.Rows.Count = 0 Then
            MessageBox.Show("Wybierz ofertę", "WIREMET")
        Else
            If mvarUzytkownik.Uprawnienia("biuro") <> 0 Then
                Dim lvarId As Integer
                lvarId = CInt(dgvOferty.SelectedRows.Item(0).Cells("colIdOferty").Value)
                Dim lvarFrm As New frmOferty(lvarId, False)
                With lvarFrm
                    .MdiParent = mdiMain.ActiveForm
                    .Show()
                End With
            End If
        End If
    End Sub


    Private Sub cmdNowaOferta_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdNowaOferta.Click

        Dim lvarFrm As New frmOferty(0, False)

        With lvarFrm
            .MdiParent = mdiMain.ActiveForm
            .Show()
        End With

    End Sub

    Private Sub cmdEdycjaOferty_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdEdycjaOferty.Click

        If dgvOferty.SelectedRows.Count = 0 Then
            MessageBox.Show("Wybierz ofertę.", "WIREMET")
        Else
            Dim lvarId As Integer
            lvarId = CInt(dgvOferty.SelectedRows.Item(0).Cells("colIdOferty").Value)
            Dim lvarFrm As New frmOferty(lvarId, True)
            With lvarFrm
                .MdiParent = mdiMain.ActiveForm
                .Show()
            End With
        End If
    End Sub

    Private Sub cmdKopiuj_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdKopiuj.Click

        If dgvOferty.SelectedRows.Count = 0 Then
            MessageBox.Show("Wybierz ofertę.", "WIREMET")
        Else
            Dim lvarId As Integer
            lvarId = CInt(dgvOferty.SelectedRows.Item(0).Cells("colIdOferty").Value)
            Dim lvarFrm As New frmOferty(lvarId, True)
            With lvarFrm
                .MdiParent = mdiMain.ActiveForm
                .Show()
            End With
        End If
    End Sub

    Private Sub cmdUtworzZlecenie_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdUtworzZlecenie.Click
        If dgvOferty.SelectedRows.Count = 0 Then
            MessageBox.Show("Wybierz ofertę.", "WIREMET")
        ElseIf CStr(dgvOferty.SelectedRows.Item(0).Cells("colStatusOferty").Value) = "nowa" Then

            MessageBox.Show("Przed utworzeniem zlecenia wyślij ofertę.", "WIREMET")
        Else
            Dim lvarId As Integer
            lvarId = CInt(dgvOferty.SelectedRows.Item(0).Cells("colIdOferty").Value)
            Dim lvarFrm As New frmZlecenia(lvarId, 3)
            With lvarFrm
                .MdiParent = mdiMain.ActiveForm
                .Show()
            End With
        End If
    End Sub

    Private Sub cmdWyczysc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdWyczysc.Click

        dgvOferty.Rows.Clear()

    End Sub


    Private Sub frmOfertySzukaj_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        dtpDataOfertyOd.Value = New Date(Today.Year, Today.Month, 1, 0, 0, 0)
        mvarCommand = " status_oferty_id <> 3 and status_oferty_id <> 4 "
    End Sub

    Private Sub frmSzukajOferty_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        mvarDbHolder.Close()
    End Sub

#Region "Changed"

    Private Sub dtpDataOfertyOd_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dtpDataOfertyOd.TextChanged
        chbDataOferty.Checked = True
    End Sub

    Private Sub dtpDataOfertyDo_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dtpDataOfertyDo.TextChanged
        chbDataOferty.Checked = True
    End Sub

#End Region

End Class
